﻿using Microsoft.Extensions.Logging;
using PHMEE.EnergyEfficiency;
using PHMEE.ToolKits;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;

namespace PHMEE
{
    public class RealTimeAppService : PHMEEAppService, IRealTimeAppService
    {
        private readonly ILogger<RealTimeAppService> _logger;
        private readonly RedisHelper _redisHelper;
        private readonly IMassFlowMeterRepository massFlowMeterRepository;

        public RealTimeAppService(ILogger<RealTimeAppService> logger, RedisHelper redisHelper, IMassFlowMeterRepository massFlowMeterRepository)
        {
            _logger = logger;
            _redisHelper = redisHelper;
            this.massFlowMeterRepository = massFlowMeterRepository;
        }

        /// <summary>
        /// 获取缓存中GPS信息
        /// </summary>
        /// <returns></returns>
        public async Task<GpsRealtimeDto> GetGpsCacheDataAsync()
        {
            var gpsDataJson = await _redisHelper.HashGetAsync("PhmeeRealTimeGpsData", "data");

            if (string.IsNullOrEmpty(gpsDataJson))
            {
                return null;
            }
            var _gpsCacheData = JsonSerializer.Deserialize<GpsCacheData>(gpsDataJson);

            string latitudeDM = _gpsCacheData.Latitude.LatitudeToDMS();
            string longitudeDM = _gpsCacheData.Longitude.LongitudeToDMS();

            return new GpsRealtimeDto
            {
                //经度
                Longitude = _gpsCacheData.Longitude,
                LongitudeDM = longitudeDM,
                //纬度
                Latitude = _gpsCacheData.Latitude,
                LatitudeDM = latitudeDM,
                //对地速度
                GroundSpeed = _gpsCacheData.GroundSpeed
            };
        }

        /// <summary>
        /// 获取每个流量计的数据
        /// </summary>
        /// <param name="VesselId">船舶id</param>
        /// <param name="onVessel">船端还是岸端</param>
        /// <returns></returns>
        public async Task<List<MassFlowmeterDto>> GetFlowmeterDataAsync(Guid VesselId, bool onVessel)
        {
            //如果是船端，不显示每分钟的油耗（实时的没法计算）
            if (onVessel)
            {
                var json = await _redisHelper.GetStringAsync("PhmeeOilFuelDatas");
                if (string.IsNullOrEmpty(json))
                {
                    _logger.LogWarning("PhmeeOilFuelDatas 不存在或者没有数据.");
                    return null;
                }
                return JsonSerializer.Deserialize<List<MassFlowmeterDto>>(json);
            }
            else
            {
                var flowmeters = await massFlowMeterRepository.GetAllFlowmeterAsync(VesselId);
                return ObjectMapper.Map<List<MassFlowmeter>, List<MassFlowmeterDto>>(flowmeters);
            }
        }

        /// <summary>
        /// 获取主机，副机，锅炉等油耗
        /// </summary>
        /// <param name="VesselId"></param>
        /// <returns></returns>
        public async Task<List<DeviceFuelConsumptionDto>> GetOilFuelDataAsync(Guid VesselId)
        {
            var oilFuels = await massFlowMeterRepository.GetDeviceFuelConsumptionAsync(VesselId);

            return ObjectMapper.Map<List<DeviceFuelConsumption>, List<DeviceFuelConsumptionDto>>(oilFuels);
        }
    }
}
